package edu.cth.stud.accelerateit.view.android;

import java.beans.PropertyChangeEvent;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import edu.cth.stud.accelerateit.R;
import edu.cth.stud.accelerateit.view.GUIController;

public class OptionsActivity extends Activity implements IAndroidGUIComponent {

	private RadioGroup radioGroup;
	private RadioButton redRB;
	private RadioButton whiteRB;
	private RadioButton blackRB;
	
	private TextView optionsHeader;
	private TextView chooseBackgroundText;
	
	private static String PREFS_NAME = "WordFinderSettings";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// link content, for code structure:
		linkContent();

		// load the application settings:
		loadSettings();
		
		// Register this activity/view with the GUIController:
		GUIController.getInstance().registerListener(this);
	}

	private void linkContent() {

		// set the view to options_view.xml:
		setContentView(R.layout.options_view);
		
		// Connect textviews:
		chooseBackgroundText = (TextView) findViewById(R.id.backgroundColor);
		optionsHeader = (TextView) findViewById(R.id.optionsHeader);
		
		// Connect radioButtons and radioGroup
		radioGroup = (RadioGroup) this.findViewById(R.id.colorRadioGroup);
		redRB = (RadioButton) this.findViewById(R.id.redRB);
		whiteRB = (RadioButton) this.findViewById(R.id.whiteRB);
		blackRB = (RadioButton) this.findViewById(R.id.blackRB);
		
		
		this.radioGroup
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						changeColor(checkedId);
					}

				});
	}

	@Override
	public void propertyChange(PropertyChangeEvent event) {
		// If the event was a shutdown command
		if (event.getPropertyName().equals("Shutdown")) {
			GUIController.getInstance().unregisterListener(this);
			finish();
		}
	}

	private void goBack() {
		// Required to keep down the activity count:
		finish();
		GUIController.getInstance().unregisterListener(this);

		Intent showMainMenu = new Intent(this.getApplicationContext(),
				WordFinderActivity.class);
		startActivity(showMainMenu);
	}

	/**
	 * This method is entered when the device's user hits the "back" button.
	 * TODO: if the back button is pressed in any non-main-menu screen, the
	 * application is to behave in a certain way. Find the requirement ID for
	 * this and enter it here in this comment field.
	 */
	@Override
	public void onBackPressed() {
		goBack();
	}

	private void changeColor(int checkedId){
		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
		SharedPreferences.Editor editor = settings.edit();
		switch(checkedId){
		case R.id.blackRB:
			editor.putInt("backgroundColor", Color.BLACK);
			editor.putInt("textColor", Color.WHITE);
			break;
		case R.id.redRB:
			editor.putInt("backgroundColor", Color.RED);
			editor.putInt("textColor", Color.WHITE);
			break;
		case R.id.whiteRB:
			editor.putInt("backgroundColor", Color.WHITE);
			editor.putInt("textColor", Color.BLACK);
			break;
		}
		editor.commit();
		loadSettings();
	}

	private void loadSettings() {
		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
		int textColor = settings.getInt("textColor", Color.WHITE);
		int backgroundColor = settings.getInt("backgroundColor", Color.BLACK);
		optionsHeader.setTextColor(textColor);
		chooseBackgroundText.setTextColor(textColor);
		redRB.setTextColor(textColor);
		blackRB.setTextColor(textColor);
		whiteRB.setTextColor(textColor);
		if (backgroundColor == Color.BLACK){
			blackRB.toggle();
		} else if(backgroundColor == Color.WHITE){
			whiteRB.toggle();
		} else if(backgroundColor == Color.RED){
			redRB.toggle();
		}
		
		
		// Set the backgroundcolor
		this.findViewById(R.id.optionScreen).setBackgroundColor(backgroundColor);
	}
}
